//
//  YGArticleViewController.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/5/3.
//

#import "YGArticleViewController.h"
#import "YGArticleBLL.h"
#import "YGArticleTableViewCell.h"
#import <MJRefresh/MJRefresh.h>
#import "YGArticleHeaderView.h"
#import "YGScienceArticleDetailViewController.h"

@interface YGArticleViewController () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView  *tableView;
@property (nonatomic, assign) NSInteger  page;
@property (nonatomic, strong) NSMutableArray  *dataArray;
@property (nonatomic, strong) YGArticleHeaderView  *headerView;

@end
@interface YGArticleViewController ()

@end

@implementation YGArticleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self requestData];
    self.title = self.name;
    // Do any additional setup after loading the view.
}

- (void)initView{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.bottom.mas_equalTo(0);
    }];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(200/375.0 * self.view.width);
        make.width.mas_equalTo(self.view.mas_width);
    }];
}

- (void)requestData{
    WeakSelf(self);
    [YGArticleBLL requestArticleWithID:self.typeId name:self.name page:self.page Success:^(YGArticleListModel * _Nonnull model) {
        [weakself.dataArray addObjectsFromArray:model.article];
        [weakself.tableView reloadData];
        weakself.page += 1;
        [weakself.headerView configureModel:model];
        [weakself.tableView.mj_footer endRefreshingCompletionBlock];
        [weakself.tableView.mj_header endRefreshingCompletionBlock];
        if (weakself.page >= model.totalPage) {
            [weakself.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
//            [weakself.tableView.mj_footer ]
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)refreshRequest{
    self.page = 1;
    WeakSelf(self);
    [YGArticleBLL requestArticleWithID:self.typeId name:@"" page:self.page Success:^(YGArticleListModel * _Nonnull model) {
        [weakself.dataArray removeAllObjects];
        [weakself.dataArray addObjectsFromArray:model.article];
        [weakself.tableView reloadData];
        [weakself.tableView.mj_footer endRefreshingCompletionBlock];
        [weakself.tableView.mj_header endRefreshingCompletionBlock];

        weakself.page += 1;
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YGScienceArticleDetailViewController *vc = [[YGScienceArticleDetailViewController alloc]init];
    YGArticleDetailModel *model;
    model = [self.dataArray objectAtIndex:indexPath.row];
    vc.id = [model valueForKey:@"id"];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)subscribei{
    [YGArticleBLL requestSubscribeithID:self.typeId Success:nil fail:nil];
   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YGArticleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(YGArticleTableViewCell.class)];
    YGArticleDetailModel *model = [self.dataArray objectAtIndex:indexPath.row];
    if (!cell) {
        cell = [[YGArticleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(YGArticleTableViewCell.class)];
    }
    [cell configureModel:model];
   
    return cell;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView registerClass:YGArticleTableViewCell.class forCellReuseIdentifier:NSStringFromClass(YGArticleTableViewCell.class)];
        WeakSelf(self);
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakself refreshRequest];
        }];
        _tableView.mj_header = header;
        _tableView.tableHeaderView = self.headerView;
        _tableView.mj_footer = [MJRefreshFooter footerWithRefreshingBlock:^{
            [weakself requestData];
        }];
    }
    return _tableView;
}

- (YGArticleHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[YGArticleHeaderView alloc] initWithFrame:CGRectZero];
        WeakSelf(self);
        _headerView.ClickBlock = ^(BOOL sub) {
            [weakself subscribei];
        };
    }
    return _headerView;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSInteger)page{
    if (_page == 0) {
        _page = 1;
    }
    return _page;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
